package cn.com.goldhs.architecture.inter.impl;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;

import cn.com.goldhs.architecture.RelationShipTypes;
import cn.com.goldhs.architecture.inter.Base;
import cn.com.goldhs.architecture.inter.Department;

public class DepartmentImpl extends BaseImpl implements
		Department {

	public DepartmentImpl(Node node) {
		super(node);
	}

	public DepartmentImpl(Node node, String name) {
		super(node, name);
	}
	
	public void moveToBase(Base base) {
		Iterable<Relationship> rels = this.getUnderlyingContainer()
				.getRelationships(Direction.INCOMING);

		for (Relationship rel : rels) {
			rel.delete();
		}

		base.addSubDept(this);
		
	}

	@Override
	public Base getParent() {
		if(this.getUnderlyingContainer().hasRelationship(Direction.INCOMING, RelationShipTypes.SubDept)) {
			Relationship rel = this.getUnderlyingContainer().getRelationships(Direction.INCOMING, RelationShipTypes.SubDept).iterator().next();
			if(rel != null) {
				return new BaseImpl(rel.getStartNode());
			}
		}
		return null;
	}
	
}
